package top.szhome.develop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.szhome.develop.dto.PropertiesSettingDTO;
import top.szhome.framework.starter.db.support.SimpleServiceImpl;
import top.szhome.develop.convert.PropertyConverter;
import top.szhome.develop.dto.PropertyDTO;
import top.szhome.develop.entity.Property;
import top.szhome.develop.mapper.PropertyMapper;
import top.szhome.develop.service.PropertyService;
import top.szhome.develop.vo.PropertyVO;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 属性服务实现
 *
 * @author 唐加利
 * @date 2023/08/21
 **/
@AllArgsConstructor
@Service
@Slf4j
public class PropertyServiceImpl extends SimpleServiceImpl<PropertyMapper, PropertyConverter, PropertyDTO, Property, PropertyVO> implements PropertyService {

    @Override
    public List<Property> getProperties(Long modelId) {
        LambdaQueryWrapper query = Wrappers.lambdaQuery(Property.class).eq(Property::getModelId, modelId);
        return getBaseMapper().selectList(query);
    }

    @Override
    public void setting(PropertiesSettingDTO settings) {
        Long modelId = settings.getModelId();

        List<Property> settingList = settings.getProperties().stream().map(getBaseConvert()::from).map(property -> {
            // 新增时设置模型ID
            if(property.getModelId() == null || property.getModelId().equals(0L)){
                property.setModelId(modelId);
            }
            return property;
        }).collect(Collectors.toList());
        saveOrUpdateBatch(settingList);
    }

}
